package com.jzy.gulimall.product.web;

import com.jzy.gulimall.product.entity.CategoryEntity;
import com.jzy.gulimall.product.service.CategoryService;
import com.jzy.gulimall.product.vo.web.Catelog2Vo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author jzy
 * @date 2020/9/12 11:17
 */
@Controller
public class IndexController {

    @Resource
    private CategoryService categoryService;

    /**
     * 首页 查出所有的一级分类
     */
    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){
        //查出所有的一级分类
        List<CategoryEntity> level1Category = categoryService.findLevel1Category();
        model.addAttribute("categorys", level1Category);
        return "index";
    }

    /**
     * 首页 封装 二级 三级分类数据
     * index/catalog.json
     * 优化: 将数据库的多次查询变为1次
     * 优化: 引入redis缓存
     */
    @RequestMapping("/index/catalog.json")
    @ResponseBody
    public Map<String, List<Catelog2Vo>> getCatalogJson(){
        return categoryService.getCatalogJson();
    }
}
